package com.shntec.temp.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shntec.bp.json.JSONArray;
import com.shntec.bp.json.JSONException;
import com.shntec.bp.json.JSONObject;

import com.shntec.bp.common.ShntecBaseAction;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.common.JSONMessageConstant;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.util.DatabaseManager;
import com.shntec.bp.util.ShntecLogger;
import com.shntec.temp.util.MySQLConn;

public class AddActionItemAction extends ShntecBaseAction {

	public AddActionItemAction () {
		super.actionCode = 20008;
		super.actionName = "AddActionItemAction";
		super.actionDescription = "增加或者修改action";
		// No need authentication
		super.needAuthenticationCheck = false;
	}

	@Override
	public ResponseMessageGenerator handleAction(RequestMessageParser parser) {

		ResponseMessageGenerator generator = new ResponseMessageGenerator();

		JSONObject payload = null;
		JSONObject parameter = parser.getParameterJsonObject();
		
		
		if (parameter == null) {

			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER);
		}
		int aid = -1;
		String actionName = null;
		String actionCode = null;
		String actionDesc = null;
		String actionRequest = null;
		String actionSuccessPayload = null;
		String actionErrorPayload = null;
		
	
		try {
			actionName = parameter.getString("actionName");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionName\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"datevalue\"");
		}
		
		try {
			actionCode = parameter.getString("actionCode");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionCode\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"actionCode\"");
		}
		
		try {
			actionDesc = parameter.getString("actionDesc");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionDesc\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"actionDesc\"");
		}
		
		try {
			actionRequest = parameter.getString("actionRequest");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionRequest\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"actionRequest\"");
		}
		
		try {
			actionSuccessPayload = parameter.getString("actionSuccessPayload");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionSuccessPayload\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"actionSuccessPayload\"");
		}
		
		try {
			actionErrorPayload = parameter.getString("actionErrorPayload");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"actionErrorPayload\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"actionErrorPayload\"");
		}
		
		try {
			aid = parameter.getInt("aid");
		} catch (JSONException e) {
			
		}
		
		MySQLConn mysql = new MySQLConn();
		Connection conn = null;
		payload = new JSONObject();
		PreparedStatement pstmt = null;
		
		
		try {
			conn = mysql.getConn();
			if(aid != -1){
				pstmt = conn.prepareStatement("update temp_actions set actionName = ?,actionCode = ?,actionDesc = ?,actionRequest = ?,actionSuccessPayload = ?,actionErrorPayload = ? where aid = ?");
				pstmt.setInt(7, aid);
			}else{
				pstmt = conn.prepareStatement("insert into temp_actions(actionName,actionCode,actionDesc,actionRequest,actionSuccessPayload,actionErrorPayload) values(?,?,?,?,?,?)");
			}
			
			
			pstmt.setString(1, actionName);
			pstmt.setString(2, actionCode);
			pstmt.setString(3, actionDesc);
			pstmt.setString(4, actionRequest);
			pstmt.setString(5, actionSuccessPayload);
			pstmt.setString(6, actionErrorPayload);
			
			
			int result = pstmt.executeUpdate();
			
			
			
			if(result > 0){
				
				return generator.toSuccess(parser, payload);
			}else{
				return generator.toError(parser, 20021, "增加时出错！");
			}
			
			
			
		} catch (ClassNotFoundException e) {
			ShntecLogger.logger.error("ClassNotFoundException" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"ClassNotFoundException");
		} catch (SQLException e) {
			ShntecLogger.logger.error("SQLException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"SQLException");
		}finally{
			if(conn != null){
				
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
		
	}

}
